<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~    http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  ~
  -->

<div class="general-options-panel" fxLayout="column">
    <span class="general-options-header">Unit Transformation</span>

    <sp-ep-settings-section
        sectionTitle="Unit conversion"
        sectionDescription="Converts the field value from the given measurement unit to the provided target unit"
        fxFlex="100"
        *ngIf="isPrimitiveProperty && !isTimestampProperty && isNumericProperty"
    >
        <div fxLayout="row">
            <div class="w-100" fxLayoutAlign="start center" fxFlex="45">
                <mat-form-field class="w-100" color="accent" fxFlex="100">
                    <input
                        matInput
                        placeholder="Unit"
                        [matAutocomplete]="auto"
                        [formControl]="currentUnitStateCtrl"
                        [compareWith]="compareFn"
                        [attr.disabled]="transformUnitEnable ? '' : null"
                        data-cy="connect-schema-unit-from-dropdown"
                    />
                    <mat-autocomplete
                        #auto="matAutocomplete"
                        [panelWidth]="'300px'"
                    >
                        <mat-option
                            *ngFor="let unit of filteredUnits | async"
                            [value]="unit.label"
                            [attr.data-cy]="unit.resource"
                            (click)="changeSourceUnit(unit)"
                        >
                            {{ unit.label }}
                        </mat-option>
                    </mat-autocomplete>
                </mat-form-field>
            </div>
            <div fxFlex="10" fxLayoutAlign="center center">
                <button
                    mat-button
                    [disabled]="!this.cachedProperty.measurementUnit"
                    (click)="transformUnit()"
                    color="accent"
                    class="form-group"
                    fxFlexAlign="center"
                    style="min-width: 60px; max-width: 60px"
                    data-cy="connect-schema-unit-transform-btn"
                >
                    <mat-icon *ngIf="!transformUnitEnable"
                        >arrow_forward</mat-icon
                    >
                    <mat-icon
                        *ngIf="transformUnitEnable"
                        style="transform: rotate(180deg)"
                        >arrow_forward</mat-icon
                    >
                </button>
            </div>
            <div fxFlex="45" fxLayoutAlign="start center">
                <mat-form-field
                    class="w-100"
                    *ngIf="transformUnitEnable"
                    color="accent"
                    fxFlex="100"
                >
                    <mat-select
                        placeholder="New Unit"
                        [(ngModel)]="selectUnit"
                        [formControl]="newUnitStateCtrl"
                        [compareWith]="compareFn"
                        data-cy="connect-schema-unit-to-dropdown"
                    >
                        <mat-option
                            *ngFor="let unit of possibleUnitTransformations"
                            [value]="unit"
                            (click)="changeTargetUnit(unit)"
                        >
                            {{ unit.label }}
                        </mat-option>
                    </mat-select>
                </mat-form-field>
            </div>
        </div>
    </sp-ep-settings-section>
</div>
